package controller;

import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import pagemodel.ActorGrid;
import pagemodel.MSG;
import pagemodel.StudentGrid;
import po.Actor;
import po.Student;
import service.ActorService;
import service.StudentService;

@Api(tags = "演员接口")
@Controller
public class ActorController {
	//ActorController.class.getName()
	private static Logger log = LogManager.getLogger(ActorController.class.getName());
	
	/*@Autowired
	private ActorService actorservice;*/
	@Autowired
	private StudentService actorservice;
	
	@ApiOperation("获取所有演员列表")
	@RequestMapping(value="/actors",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public StudentGrid listActors(@RequestParam("current") int current,@RequestParam("rowCount") int rowCount,
			@RequestParam(required=false) String firstName,@RequestParam(required=false) String lastName){
			
		log.info("aaa");
		log.error("bbb");
		int total;
		List<Student> list;
		
		//ActorGrid grid=new ActorGrid();
		StudentGrid grid = new StudentGrid();
		
		if (firstName==null&&lastName==null) {
			total=actorservice.getactornum();
			//total = studentservice.getactornum();
			list=actorservice.queryAllStduent(current,rowCount);
			for(Student s:list) {
				System.out.println(s);
			}
			grid.setRows(list);
			grid.setTotal(total);
		} else {
			/*Actor a = new Actor();
			a.setFirst_name(firstName);
			a.setLast_name(lastName);*/
			Student stu = new Student();
			stu.setC_name(firstName);
			stu.setC_address(lastName);
			
			total = actorservice.queryByname(stu).size();
			list = actorservice.queryByname(stu, current, rowCount);
			grid.setRows(list);
			grid.setTotal(total);
		}
		grid.setCurrent(current);
		grid.setRowCount(rowCount);
		
		return grid;
	}
	
	
	
	@ApiOperation("获取所有演员列表XML")
	@RequestMapping(value="/listActorsXml",produces = {"application/xml;charset=UTF-8"},method = RequestMethod.GET)
	@ResponseBody
	public StudentGrid listActorsXml(@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
		int total=actorservice.getactornum();
		List<Student> list=actorservice.queryAllStduent(current,rowCount);
//		ActorGrid grid=new ActorGrid();
		StudentGrid grid = new StudentGrid();
		grid.setCurrent(current);
		grid.setRowCount(rowCount);
		grid.setRows(list);
		grid.setTotal(total);
		return grid;
	}
	
	
	@RequestMapping(value="/showactor",method = RequestMethod.GET)
	public String showactor(){
		return "/html/showactor.html";
	}
	/**
	 * 
	 *@作者:
	 *@简介:修改信息
	 * @param id
	 * @param a
	 * @return
	 */
	@ApiOperation("修改一个演员")
	@RequestMapping(value="/actors/{id}",method = RequestMethod.PUT,consumes="application/json")
	@ResponseBody
	public Student updateactor(@PathVariable("id") Integer id, @RequestBody /*Actor a*/ Student s){
		System.out.println(s.toString());
		//Actor actor=actorservice.updateactor(a);
		Student stu = actorservice.updateStudent(s);
		return s;
	}
	
	@ApiOperation("获取一个演员")
	@RequestMapping(value="/actors/{id}",method = RequestMethod.GET)
	@ResponseBody
	public MSG getactorbyid(@PathVariable("id") /*short*/Integer id){
		//Actor a=actorservice.getActorByid(id);
		Student stu = actorservice.queryByid(id);
		return new MSG("200",stu);
	}
	
	@ApiOperation("添加一个演员")
	@RequestMapping(value="/actors",method = RequestMethod.POST)
	@ResponseBody
	public Student add(@RequestBody /*Actor*/Student s){
		//Actor actor=actorservice.addactor(a);
		System.out.print(s.getC_birthday());
		Student stu = actorservice.insertStudent(s);
		return stu;
	}
	
	@ApiOperation("删除一个演员")
	@RequestMapping(value="/deletectors/{id}",method = RequestMethod.GET)
	public String delete(@PathVariable("id") /*short id*/Integer id){
		//actorservice.delete(id);
		System.out.println("进来的");
		actorservice.deleteStudent(id);
		return "/html/showactor.html";
	}
	
	@ApiOperation("把演员导出为Excel")
	@RequestMapping(value="/exportactor",method = RequestMethod.GET)
	public void export(HttpServletResponse response) throws Exception{
		InputStream is=actorservice.getInputStream();
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("contentDisposition", "attachment;filename=AllUsers.xls");
		ServletOutputStream output = response.getOutputStream();
		IOUtils.copy(is, output);
	}
	
	@ApiOperation("自定义请求")
	@RequestMapping(value="/custom",method = RequestMethod.GET)
	@ResponseBody
	public Student custom(String username, Integer password){
		System.out.println(username);
		System.out.println(password);
		String jsonstr = "{name:\""+username+"\"password:"+password+"}";
		Student stu = new Student();
		
		stu.setC_id(password);
		stu.setC_name(username);
		return stu;
	}
}
